package com.hn658.base.message.backend.action.queue.impl;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hn658.base.message.appinfo.entity.AppMqConfigEO;
import com.hn658.base.message.appinfo.service.IAppMqService;
import com.hn658.base.message.backend.action.queue.IAppMqAction;
import com.hn658.base.message.backend.action.queue.vo.AppMqRequest;
import com.hn658.base.message.backend.action.queue.vo.AppMqResponse;
import com.hn658.base.message.common.utils.DateUtil;
import com.hn658.framework.common.AbstractBaseService;
import com.hn658.framework.common.IServiceResponse;
import com.hn658.framework.common.ResponseStatus;
import com.hn658.framework.dataaccess.pagination.GenericPage;
import com.hn658.framework.shared.exception.BusinessException;

/**
 * 应用消息队列管理action
 */
@Controller("appMqAction")
@Path("appMq")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public class AppMqActionImpl extends AbstractBaseService implements IAppMqAction{

	
	@Autowired
	private IAppMqService appMqService;
	
    @Path("queryQueue")
    @Consumes("application/x-www-form-urlencoded; charset=UTF-8")
    @Produces("application/json; charset=UTF-8")
    @POST
    public IServiceResponse queryQueue(
            @FormParam(value="appId") String appId,
            @FormParam(value="queueNumber") String queueNumber,
            @FormParam(value="queueName") String queueName,
            @FormParam(value="queuePriority") String queuePriority,
            @FormParam(value="createStartTime") String createStartTime,
            @FormParam(value="createEndTime") String createEndTime,
            @FormParam(value="limit") int limit,
            @FormParam(value="start") int start) throws Exception {
        AppMqResponse response=new AppMqResponse();
        Map<String,Object> params=new HashMap<String, Object>();
        if(StringUtils.isNotBlank(appId)){
            params.put("appId", appId);
        }
        if(StringUtils.isNotBlank(queueNumber)){
            params.put("queueNumber", queueNumber);
        }
        if(StringUtils.isNotBlank(queueName)){
        	params.put("queueName", queueName);
        }
        try {
            params.put("createStartTime",
                    StringUtils.isEmpty(createStartTime)?null:
                            DateUtils.parseDate(createStartTime, "yyyy-MM-dd"));
            params.put("createEndTime",
                    StringUtils.isEmpty(createEndTime)?null:
                            DateUtil.oneDateLastTime(DateUtils.parseDate(createEndTime, "yyyy-MM-dd")));
            GenericPage<AppMqConfigEO> appMqPage = appMqService.queryAppMq(params, limit, start, "create_time", false);
            response.setQueueList(appMqPage.getData());
            response.setTotalCount(0L);
            this.success(response);
        } catch (BusinessException e) {
            this.error(response, e);
        }
        return response;
    }


    /**
     * 增加应用队列
     * @return
     */
    @Path("addAppMq")
    @POST
    public IServiceResponse addAppMq(AppMqRequest request){
    	AppMqResponse response=new AppMqResponse();
        try {
        	appMqService.addAppMq(request.getAppMq());
            this.success(response);
        } catch (BusinessException e) {
            this.error(response, e);
            if (e.getErrorMsg()==e.getErrorCode()) {
                response.setResponseStatus(new ResponseStatus(e.getErrorCode(), e.getErrorMsg()));
            }
        }
        return response;
    }
    /**
     * 修改队列
     * @param request
     * @return
     */
    @Path("modifyAppMq")
    @POST
    public IServiceResponse modifyAppMq(AppMqRequest request){
    	AppMqResponse response=new AppMqResponse();
        try {
        	appMqService.modifyAppMq(request.getAppMq());
            this.success(response);
        } catch (BusinessException e) {
            this.error(response, e);
            if (e.getErrorMsg()==e.getErrorCode()) {
                response.setResponseStatus(new ResponseStatus(e.getErrorCode(), e.getErrorMsg()));
            }
        }
        return response;
    }
    /**
     * 启用队列
     * @param request
     * @return
     */
    @Path("enableAppMq")
	@POST
	public IServiceResponse enableAppMq(AppMqRequest request) {
    	AppMqResponse response=new AppMqResponse();
		try{
			appMqService.enableAppMq(request.getId());
			this.success(response);
		}catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	/**
	 * 禁用应用
	 * @return
	 */
	@Path("disableAppMq")
	@POST
	public IServiceResponse disableAppMq(AppMqRequest request) {
		AppMqResponse response=new AppMqResponse();
		try{
			appMqService.disableAppMq(request.getId());
			this.success(response);
		}catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
}
